import dayjs from 'dayjs'

/**
 * @desc 生成总路由
 * @param context
 * @param ignore
 */
export const getRoutes = (context: any, ignore: any) => {
  const children: any = []
  Object.keys(context).forEach(key => {
    if (!ignore.includes(key)) {
      try {
        const arr = context[key].default
        if (arr && arr.length) {
          children.push(...arr)
        }
      } catch (e) {
        console.error(e)
      }
    }
  })
  return children
}


// 千分符，整数
export const thousands = (num: number) => {
  if (typeof num !== 'number' && !num) {
    return ''
  }
  return (+num).toLocaleString()
}

// 千分符，格式两位小数
export const decimalpre = (num: number, precision = 2) => {
  if (typeof num !== 'number' && !num) {
    return ''
  }
  return `${(+num).toFixed(precision).replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,')}`
}


export const date = (d: string, fmt = 'DD/MM/YYYY') => {
  if (!d) return ''

  return dayjs(new Date(d)).format(fmt)
}


export const getUesrId = () => {
  return localStorage.getItem('exuserId') || ''
}
